package com.example.rqweb.Okhttp;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rqweb.R;

import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class okhttp extends AppCompatActivity
{
        Response res;
        Button b1,b,b2,b3;
        TextView t1;
        okhttp op;
        //创建客户端连接对象
           OkHttpClient htp=new OkHttpClient.Builder(). connectTimeout(1500, TimeUnit.SECONDS).

           retryOnConnectionFailure(true).build();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ok_post);
        initb();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tb();
            }
        });
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yb();
            }
        });
    }
   //同步请求

    public  void initb()
    {


        b=findViewById(R.id.t);
        b1=findViewById(R.id.t1);
        t1=findViewById(R.id.result);

    }

   public void  tb()
   {

      Request requ=new Request.Builder().get().url("https://www.baidu.com").build();

         try {
            res = htp.newCall(requ).execute();

         }
         catch (IOException e) {
            e.printStackTrace();
         }
       t1.setText( res.body().toString());
      }


   public void yb() {

      Request requ = new Request.Builder().get().url("http://www.baidu.com").build();

      {

         htp.newCall(requ).enqueue(new Callback() {

            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
               System.out.println("调用失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException
            {

               // System.out.println(response.body());
                Log.i("SW","onResponse: "+response.body().string());


            }
         });

      }
   }



//同步方式提交图片
   public void tbimag()
   {


      //获取sd卡中的文件
            File file = new File(Environment.getExternalStorageDirectory()+"/t1.jpg");
      //设置请求参数
           MultipartBody.Builder builder = new MultipartBody.Builder()
              //设置类型
              .setType(MultipartBody.FORM)
              //设置请求数据的格式
              .addFormDataPart("img", "HeadPortrait.jpg",
                      RequestBody.create(MediaType.parse("image/png"), file));

      RequestBody requestBody = builder.build();

      Request request = new Request.Builder()
              .url("www.baidu.com")
              .post(requestBody)
              .build();

      Call call = htp.newCall(request);
      try {
         call.execute();
      } catch (IOException e) {
         e.printStackTrace();
      }

   }
    public void ybimag()
    {


        //获取sd卡中的文件
        File file = new File(Environment.getExternalStorageDirectory()+"/t1.jpg");
        MultipartBody.Builder builder = new MultipartBody.Builder()
                //设置类型
                .setType(MultipartBody.FORM)
                //设置正文内容
                .addFormDataPart("img", "HeadPortrait.jpg",
                        RequestBody.create(MediaType.parse("image/png"), file));

        RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url("www.baidu.com")
                .post(requestBody)
                .build();

        Call call = htp.newCall(request);

            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    System.out.println(response.body().toString());

                }
            });
        }

   }









